import lib.system.mlf.FlowProcessor;

var flowProcessor=FlowProcessor.getInstance();

function loadBookModification(target)
{
	Packages.system.mlf.GateKeeper.updateDynamicPortlet(target._parent, "app/vo/BookModify.xml");
	dynamicPortletArr.bookInfo="bookModify";
	if (displayMode.bookInfo!="expand")
	{	
		var cellPortlet=Packages.system.mlf.GateKeeper.scanElement(this._name, "bookInfo");
		Packages.system.display.JAccordion.invokeReleaseEvent(mlf.mcmanager.PathResolver.drawPath(cellPortlet.header));
	}
}

function onBookModifyInit()
{
	var evtTargetArr:Array=new Array();
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "modifyConfirmSpot"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "modifyReturnSpot"));
	Packages.system.events.StateFulButtonEvent.initialize(evtTargetArr);

	// http://localhost/jumpingNotes/restlet/books/2/versions/sub_title?sessionId=5de9cf01-4ef6-485c-afcc-28d1b676b3a5
	Packages.system.mlf.GateKeeper.addListener("noTick", this);
	Packages.system.mlf.GateKeeper.addListener("onPropertyLoad", this);
	flowProcessor.regRules("LoadingBookProperties");
	var properties:Array=["name", "sub_title", "author", "author_intro", "publish_press", "publish_year", "website", "url", "chapter_count", "word_count", "complete_status", "abstract"];
	properties.reverse();
	for (var i=0; i<properties.length; i++)
	{
		flowProcessor.regEvent("onPropertyLoad", properties[i]);
	}

	flowProcessor.launchEvent();
}

function onPropertyLoad(obj)
{
	Packages.modula.RequestCommand.request("HTTP", "restlet/books/"+bookId+"/versions/"+obj.param, null, Packages.model.vo.BookView, "append", this, {container:"bookModify", dynamicPortlet:dynamicPortletArr.bookInfo, flow:true});
}

function noTick(obj)
{
	Packages.system.mlf.GateKeeper.removeListener("noTick", this);
}

function confirmBookModify()
{
	var textUtil=Packages.system.utils.TextUtil;

	var obj:Object=new Object();
	obj.bookId=bookId;
	obj.memberId=Packages.model.UserProfile.data.memberId;
	obj.name=textUtil.getValue("value#name", this._name);
	obj.author=textUtil.getValue("value#author", this._name);
	obj.authorIntro=textUtil.getValue("value#author_intro", this._name);
	obj.subTitle=textUtil.getValue("value#sub_title", this._name);
	obj.abstract=textUtil.getValue("value#abstract", this._name);
	obj.publishPress=textUtil.getValue("value#publish_press", this._name);
	obj.publishYear=textUtil.getValue("value#publish_year", this._name);
	obj.chapterCount=textUtil.getValue("value#chapter_count", this._name);
	obj.wordCount=textUtil.getValue("value#word_count", this._name);
	obj.completeStatus=textUtil.getValue("value#complete_status", this._name);

	var path=Packages.system.mlf.scanElement(this._name, "basic");
	if (obj.completeStatus==Packages.system.mlf.scanDictionary(path, "COMPLETESTATUS_1"))
	{
		obj.completeStatus="1";
	}
	else obj.completeStatus="2";

	obj.website=textUtil.getValue("value#website", this._name);
	obj.url=textUtil.getValue("value#url", this._name);

	var data:Object=new Object();
	// data.path=target._parent;
	data.name="BookSpecial";
	data.container="bookInfoContainer";
	data.oper="bookExpectCreate";
	data.uri="app/content/bookPage/BookSpecial.xml";
	data.returnPath="loadBookSpecialInfo";
	data.dynamicPortlet=dynamicPortletArr.bookInfo;

	dynamicPortletArr.bookInfo="commonRemotingResult";

	Packages.modula.RequestCommand.request("REMOTING", "bookInfoVersionCreate", obj, Packages.model.vo.CommonRemotingResultView, "normal", this, data);
}

function selectVersion(param:String)
{
	// Packages.log("selectVersion: "+param, "debug");
	var eleArr=param.split("#");
	
	var opt_tf=Packages.system.mlf.GateKeeper.scanElement(this._name, "value#"+eleArr[2]);
	var value_tf=Packages.system.mlf.GateKeeper.scanElement(this._name, "value#"+eleArr[1]);
	value_tf.text=opt_tf.text;
	// Packages.log("opt_tf: "+opt_tf, "debug");
	// Packages.log("value_tf: "+value_tf, "debug");
}